﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CADTool.DotNetARX
{
    public static class Tools
    {
        /// <summary>
        /// 将实体添加到模型空间
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="ent">要添加的实体</param>
        /// <returns>返回添加到模型空间中的实体ObjectId</returns>
        public static ObjectId AddToModelSpace(this Database db, Entity ent)
        {
            ObjectId entId; //用于返回添加到模型空间中的实体ObjecId
            //定义一个指向当前数据库中的事务
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //以读的方式打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                //以写的方式打开模型空间块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                entId = btr.AppendEntity(ent); //将图形对象添加到块表记录中
                trans.AddNewlyCreatedDBObject(ent, true);//把对象添加到事务处理当中
                trans.Commit(); //提交事务处理
            }
            return entId;
        }
        /// <summary>
        /// 将实体添加到模型空间
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="ents">要添加的实体</param>
        /// <returns>返回添加到图纸空间中的实体ObjectId</returns>
        public static ObjectIdCollection AddToModelSpace(this Database db, params Entity[] ents)
        {
            ObjectIdCollection ids = new ObjectIdCollection();
            TransactionManager trans = db.TransactionManager;
            BlockTableRecord btr = (BlockTableRecord)trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
            foreach (var ent in ents)
            {
                ids.Add(btr.AppendEntity(ent));
                trans.AddNewlyCreatedDBObject(ent, true);
            }
            btr.DowngradeOpen();
            return ids;
        }
        /// <summary>
        /// 将实体添加到模型空间
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="ents">要添加的实体</param>
        public static void AddEntities<T>(List<T> ents) where T : Entity
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
                foreach (var item in ents)
                {
                    btr.AppendEntity(item);
                    trans.AddNewlyCreatedDBObject(item, true);
                }
                trans.Commit();
            }
        }
        /// <summary>
        /// 将实体添加到模型空间
        /// </summary>
        /// <param name="ent">要添加的实体</param>
        public static void AddEntity(Entity ent)
        {
            AddEntities(new List<Entity>() { ent });
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="ents">要删除的实体</param>
        public static void EraseEntities<T>(List<T> ents) where T : Entity
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                foreach (var item in ents)
                {
                    item.ObjectId.GetObject(OpenMode.ForWrite);
                    if (item.IsErased) continue;
                    if (item.ObjectId == ObjectId.Null) continue;
                    item.Erase();
                }
                trans.Commit();
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="ent">要删除的实体</param>
        public static void EraseEntities<T>(T ent) where T : Entity
        {
            EraseEntities(new List<T>() { ent });
        }
        public static void HigthligtEntities(this ObjectIdCollection ids) 
        {
            using (Transaction trans=Z.doc.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in ids)
                {
                    Entity ent = trans.GetObject(id,OpenMode.ForWrite) as Entity;
                    if (ent!=null)
                    {
                        ent.Highlight();//亮显实体
                    }
                }
                trans.Commit();
            }

        }

    }
}

